﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        #region Private Variable

        Dictionary<int, DevComponents.DotNetBar.SuperTabControl> listTab;
        List<DevComponents.DotNetBar.RibbonTabItem> listRbTabItem;
        RuleObjectBUS RuleBUS;

        #endregion Private Variable

        #region Constructor

        public frmMain()
        {
            InitializeComponent();
            listTab = new Dictionary<int, DevComponents.DotNetBar.SuperTabControl>();
            listRbTabItem = new List<DevComponents.DotNetBar.RibbonTabItem>();
            RuleBUS = new RuleObjectBUS();
        }

        #endregion Constructor

        #region Private Methods

        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadRbTabItem();
            //OpenParentTabPages((int)RibbonTabItemIndex.indexRbTabItem, indexRbTabItem.Name, indexRbTabItem.Text, new Resource.frmTest());
            tabMain.SelectedTabChanged += new EventHandler<DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs>(tabMain_SelectedTabChanged);
            indexRbTabItem_Click(indexRbTabItem, null);
            indexRbTabItem.Select();

            param.FrmLogin frm = new param.FrmLogin();
            frm.ShowDialog();

            if (Global.UserID > 0)
            {
                AccountBUS AccBUS = new AccountBUS();
                AccountDTO acc = AccBUS.GetAccountByID(Global.UserID);
                labelItem1.Text = string.Format("{0} , Đăng nhập lúc : {1}", acc.EmployeeName, DateTime.Now.ToString("hh:mm - dd/MM/yyyy"));
            }
            else
            {
                Application.Exit();
            }
        }

        void tabMain_SelectedTabChanged(object sender, DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs e)
        {
            string name = tabMain.SelectedTab.Name.Replace("tabP", "");
            listRbTabItem.First(t => t.Name == name).Select();
        }

        private void tabMain_TabItemClose(object sender, DevComponents.DotNetBar.SuperTabStripTabItemCloseEventArgs e)
        {
            if (tabMain.Tabs.Count > 1)
            {
                int key = int.Parse(e.Tab.Tag.ToString());
                listTab.Remove(key);
                //tabMain.SelectedTabIndex--;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void LoadRbTabItem()
        {
            listRbTabItem.Add(indexRbTabItem);
            listRbTabItem.Add(mainMenuRbTabItem);
            listRbTabItem.Add(bookRbTabItem);
            listRbTabItem.Add(CustomerRbTabItem);
            listRbTabItem.Add(EmployeeRbTabItem);
            listRbTabItem.Add(ReportRbTabItem);
            listRbTabItem.Add(settingRbTabItem);
        }

         

        #endregion Private Methods

        #region OpenTab

        public void OpenParentTabPages(int key,string pTabName, string pText,Form frm)
        {
            tabMain.SelectedTabChanged -= tabMain_SelectedTabChanged;
            string tabParentName = "tabP" + pTabName;
            if (tabMain.Tabs.Contains(tabParentName))
            {
                int index = tabMain.Tabs.IndexOf(tabParentName);
                tabMain.SelectedTabIndex = index;
            }
            else
            {
                DevComponents.DotNetBar.SuperTabControlPanel panel = new DevComponents.DotNetBar.SuperTabControlPanel();
                tabMain.Controls.Add(panel);
                panel.Dock = DockStyle.Fill;

                DevComponents.DotNetBar.SuperTabControl tabCtr = new DevComponents.DotNetBar.SuperTabControl();
                tabCtr.Name = "tabC" + pTabName;
                panel.Controls.Add(tabCtr);
                tabCtr.Dock = DockStyle.Fill;
                tabCtr.Show();                

                DevComponents.DotNetBar.SuperTabItem tab = new DevComponents.DotNetBar.SuperTabItem();
                tab.Name = tabParentName;
                tab.Text = pText;

                tab.AttachedControl = panel;
                tabMain.Tabs.Add(tab);

                listTab.Add(key, tabCtr);
                tabCtr.CloseButtonOnTabsVisible = true;
                tab.Tag = key;
                //OpenTab(key, frm);
                tabMain.SelectedTabIndex = tabMain.Tabs.Count - 1;
            }
            tabMain.SelectedTabChanged += tabMain_SelectedTabChanged;
            
        }

        public void OpenTab(int key, Form frm)
        {
            DevComponents.DotNetBar.SuperTabControl tabCtr = listTab[key];
            string tabName = "tabC" + frm.Name;
            if (tabCtr.Tabs.Contains(tabName))
            {
                int index = tabCtr.Tabs.IndexOf(tabName);
                tabCtr.SelectedTabIndex = index;
            }
            else
            {
                //DevComponents.DotNetBar.SuperTabControlPanel panel = new DevComponents.DotNetBar.SuperTabControlPanel();
                //tabMain.Controls.Add(panel);
                //panel.Dock = DockStyle.Fill;

                //DevComponents.DotNetBar.SuperTabItem tabPage = new DevComponents.DotNetBar.SuperTabItem();
                //tabPage.Name = tabName;
                //tabPage.Text = text;

                //QuanLyNhaSach.Resource.frmTest frm = new Resource.frmTest();
                //frm.TopLevel = false;

                //panel.Controls.Add(frm);
                //tabPage.AttachedControl = panel;
                //tabMain.Tabs.Add(tabPage);

                //frm.Dock = DockStyle.Fill;
                //frm.Show();

                //tabMain.SelectedTabIndex = tabMain.Tabs.Count - 1;

                DevComponents.DotNetBar.SuperTabControlPanel panel = new DevComponents.DotNetBar.SuperTabControlPanel();
                tabCtr.Controls.Add(panel);
                panel.Dock = DockStyle.Fill;

                DevComponents.DotNetBar.SuperTabItem tabPage = new DevComponents.DotNetBar.SuperTabItem();
                tabPage.Name = tabName;
                tabPage.Text = frm.Text;

                //QuanLyNhaSach.Resource.frmTest frm = new Resource.frmTest();
                frm.TopLevel = false;

                panel.Controls.Add(frm);
                tabPage.AttachedControl = panel;
                tabCtr.Tabs.Add(tabPage);
                tabCtr.TabItemClose += new EventHandler<DevComponents.DotNetBar.SuperTabStripTabItemCloseEventArgs>(tabCtr_TabItemClose);

                frm.Dock = DockStyle.Fill;
                frm.Show();

                tabCtr.SelectedTabIndex = tabCtr.Tabs.Count - 1;
            }
        }

        void tabCtr_TabItemClose(object sender, DevComponents.DotNetBar.SuperTabStripTabItemCloseEventArgs e)
        {
            DevComponents.DotNetBar.SuperTabControl ctr = (DevComponents.DotNetBar.SuperTabControl)sender;
            if(ctr.Tabs.Count <= 1)
            {
                e.Cancel = true;
            }
        }

        #endregion OpenTab           

        #region Button Item

        private void btnSellBook_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.mainMenuRbTabItem, new MainMenu.FrmInvoiceBook { Action = ActionName.Insert });
        }

        private void btnBill_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.mainMenuRbTabItem, new MainMenu.FrmPayMoney());
        }

        private void btnBook_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.mainMenuRbTabItem, new MainMenu.FrmReceivingBook {Action = ActionName.Insert });
        }

        private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            Employee.EmployeeDetail frm = new Employee.EmployeeDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        #endregion Button Item        

        #region RibbonTabItem Click

        private void indexRbTabItem_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.indexRbTabItem, indexRbTabItem.Name, indexRbTabItem.Text, null);
        }

        private void ribbonTabItem1_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.mainMenuRbTabItem, mainMenuRbTabItem.Name, mainMenuRbTabItem.Text, new Resource.frmTest());
            btnSellBook.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.SellBook);
            btnReceivingBook.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.Receiving);
            btnPaymentDebt.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.PayDebt);
        }

        private void ribbonTabItem2_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.bookRbTabItem, bookRbTabItem.Name, bookRbTabItem.Text, new Book.FrmBookList());
            btnBookList.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.BookList);
            btnInsertBook.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionBook);
            btnHightSearch.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.BookList);
            btnAuthorList.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject. AuthorList);
            btnAddAuthor.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor);
            btnCategoryList.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.CategoryList);
            btnAddCategory.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionCategory);
        }        

        private void CustomerRbTabItem_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.CustomerRbTabItem, CustomerRbTabItem.Name, CustomerRbTabItem.Text, new Customer.FrmCustomerList());
            btnCustomerList.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.CustomerList);
            btnCustomerAdd.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionCustomer);
        }

        private void EmployeeRbTabItem_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.EmployeeRbTabItem, EmployeeRbTabItem.Name, EmployeeRbTabItem.Text, new Employee.EmployeeList());
            btnListEmployee.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.EmployeeList);
            btnAddEmployee.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionEmployee);
            btnUserGroup.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.UserGroup);
            btnAddUserGroup.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionUserGroup);
            btnAccount.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.AccountList);
            btnAddAccount.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAccount);
        }

        private void ReportRbTabItem_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.ReportRbTabItem, ReportRbTabItem.Name, ReportRbTabItem.Text, new Resource.frmTest());
            btnBookReport.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.StockReport);
            btnDebtReport.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.DebtReport);
        }

        private void settingRbTabItem_Click(object sender, EventArgs e)
        {
            OpenParentTabPages((int)RibbonTabItemIndex.settingRbTabItem, settingRbTabItem.Name, settingRbTabItem.Text, new Resource.frmTest());
            btnParameterList.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.Param);
            btnDocSequence.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.DocSequence);
            btnRuleObject.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.RuleObject);
        }

        #endregion RibbonTabItem Click        

        private void buttonItem22_Click(object sender, EventArgs e)
        {
            UsersGroupDTO userG = new UsersGroupDTO { Status = true, UsersGroupCode = "QL", UsersGroupName = "Quan Ly" };
            UsersGroupBUS userGBUS = new UsersGroupBUS();
            userGBUS.InsertUsersGroup(userG);
        }

        private void btnAddAuthor_Click(object sender, EventArgs e)
        {
            Book.FrmAuthorDetail frm = new Book.FrmAuthorDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            Book.FrmCategoryDetail frm = new Book.FrmCategoryDetail { Action = ActionName.Insert };
            //Book.FrmCategoryList frmList = (Book.FrmCategoryList)Application.OpenForms["FrmCategoryList"];
            //if (frmList != null)
            //{
            //    frm.OnCategoryChanged += new Book.FrmCategoryDetail.CategoryHasChanged(frmList.CategoryChangeAfterInsert);
            //}
            frm.ShowDialog();
        }

        private void btnCategoryList_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.bookRbTabItem,new Book.FrmCategoryList());
        }

        private void btnListEmployee_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.EmployeeRbTabItem, new Employee.EmployeeList());
        }

        private void btnAuthorList_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.bookRbTabItem, new Book.FrmAuthorList());
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.bookRbTabItem, new Book.FrmBookList());
        }

        private void buttonItem14_Click(object sender, EventArgs e)
        {
            Book.FrmBookDetail frm = new Book.FrmBookDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        private void btnHightSearch_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.bookRbTabItem, new Book.FrmBookListSearch());
        }

        private void btnCustomerList_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.CustomerRbTabItem, new Customer.FrmCustomerList());
        }

        private void btnCustomerAdd_Click(object sender, EventArgs e)
        {
            Customer.FrmCustomerDetail frm = new Customer.FrmCustomerDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        private void btnParameterList_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.settingRbTabItem, new param.FrmParameterList());
        }

        private void btnInsertParameter_Click(object sender, EventArgs e)
        {
            
        }

        private void btnUserGroup_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.EmployeeRbTabItem, new Employee.FrmUserGroupList());
        }

        private void btnAddUserGroup_Click(object sender, EventArgs e)
        {
            Employee.FrmUsersGroupDetail frm = new Employee.FrmUsersGroupDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        private void btnDocSequence_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.settingRbTabItem, new param.FrmDocSequence());
        }

        private void btnBookReport_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.ReportRbTabItem, new MainMenu.FrmBookReport());
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.EmployeeRbTabItem, new Employee.FrmAccountList());
        }

        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            Employee.FrmAccountDetail frm = new Employee.FrmAccountDetail { Action = ActionName.Insert };
            frm.ShowDialog();
        }

        private void btnLogOut_Click(object sender, EventArgs e)
        {
            Global.UserID = -1;
            Global.GroupID = -1;
            indexRbTabItem_Click(sender,e);
            frmMain_Load(sender, e);

            for (int i = 1; i <= 7; i++)
            {
                if (listTab.ContainsKey(i))
                {
                    DevComponents.DotNetBar.SuperTabControl ctr = listTab[i];
                    ctr.Tabs.Clear();
                }
            }
            //listRbTabItem.Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (DevComponents.DotNetBar.MessageBoxEx.Show("Bạn muốn thoát khỏi chương trình?", "Hỏi", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnRuleObject_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.settingRbTabItem, new param.FrmRuleObject());
        }

        private void btnDebtReport_Click(object sender, EventArgs e)
        {
            OpenTab((int)RibbonTabItemIndex.ReportRbTabItem, new Report.FrmReportDebtMoney());
        }

    }
}
